#include <iostream>
#include <mysql/mysql.h>
#include "Common.hpp"
#define PASSWD "LCQ010723@lcq163.com"

bool InsertSql(std::string& sql)
{
    MYSQL* conn = mysql_init(nullptr);
    if(nullptr == mysql_real_connect(conn,"127.0.0.1","connector",PASSWD,"conn",3306,nullptr,0))
    {
        std::cerr<<"connect mysql error"<<std::endl;
        return false;
    }
    std::cerr<<"connect mysql success"<<std::endl;
    std::cerr << "query : " << sql <<std::endl;
    int ret = mysql_query(conn, sql.c_str());
    std::cerr << "result: " << ret << std::endl;
    mysql_close(conn);
    return true;
}

int main()
{
    std::string query_string;
    if(GetQueryString(query_string))
    {
        //name=tom&passwd=111111
        std::string user_name,passwd;
        CurString(query_string,user_name,passwd,"&");

        //name=tom
        std::string _name,sql_name;
        CurString(user_name,_name,sql_name,"=");

        std::string _passwd,sql_passwd;
        CurString(passwd,_passwd,sql_passwd,"=");

        std::string sql = "insert into user(name,passwd) values(\'";
        sql+=sql_name;
        sql+="\',\'";
        sql+=sql_passwd;
        sql+="\')";
        

        if(InsertSql(sql))
        {
            std::cout << "<html>";
            std::cout << "<head><meta charset=\"utf-8\"></head>";
            std::cout << "<body><h1>注册成功!</h1></body>";
        }
    }
    return 0;
}